﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AviumX.AviumCore.Hardware.Mouse
{
    class Cursor
    {
        /*
 * Note: since the buffer is always cleared to a color we really dont need to 
 * save what is behind the mouse anymore, so we are just going to draw it. =)
 */

        /*
         * Should work perfect with the new double buffer code
         * and should have no major problems. - Civilwarrock
         */

        //Handlers, used to work with the mouse.
        public static AviumX.AviumCore.Screen.Graphics.Images.Mouse CursorImage = new AviumX.AviumCore.Screen.Graphics.Images.Mouse();
        public static AviumX.AviumCore.Hardware.Mouse.Mouse Mouse = new AviumX.AviumCore.Hardware.Mouse.Mouse();
        private static Boolean Drawn = false;
        /*
         * Sates
         */
        public enum MouseState
        {
            LeftClick,
            RightClick,
            MiddleClick,
            Nothing
        }
        public static MouseState CurrentState = MouseState.Nothing;
        public static void StartCursor()
        {
            Mouse.Start();
            int x = Mouse.Getx();
            int y = Mouse.Gety();
            CursorImage.UpdateLocation(x, y);
            //Core.Screen.Screen.TheScreen.SaveFrame((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
            SaveBackBuffer();
        }
        public static void SaveBackBuffer()
        {
            //Core.Screen.Screen.TheScreen.SaveFrame((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
            //Core.Screen.Screen.TheScreen.SaveFrameFromBuffer((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
        }
        public static void DrawCursor()
        {
            /*
             * Incase the mouse has not been drawn yet,
             * and something got drawn before it and then it got drawn
             * we want to save the back buffer to prevent image loss, and
             * some other weird effects.
             */

            #region " Click Events "
            if (Mouse.CheckLeftClick() == true)
            {
                CurrentState = MouseState.LeftClick;
            }
            else if (Mouse.CheckRightClick() == true)
            {
                CurrentState = MouseState.RightClick;
            }
            else if (Mouse.CheckMiddleClick() == true)
            {
                CurrentState = MouseState.MiddleClick;
            }
            else
            {
                CurrentState = MouseState.Nothing;
            }
            #endregion
            if (Drawn == false)
            {
                SaveBackBuffer();
                Drawn = true;
            }
            //Core.Screen.Screen.TheScreen.RestoreFrame((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
            //Core.Screen.Screen.TheScreen.RestoreFrameIntoBuffer((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
            int x = Mouse.Getx();
            int y = Mouse.Gety();
            //if (CursorImage.lastx == x - 10 || CursorImage.lastx == x - 8)
            //{
            //    Core.Graphics.Font.FontHandler.Print(Graphics.Font.Font.Fonts.QuartzMS, Screen.ScreenHandler.SystemColors.Lime, "999", 0, 0);
            //    uint speed = (uint)Core.System.Throttle.GetSpeed() - 5;
            //    Core.System.Throttle.SetSpeed(speed);
            //}
            //This should prevent some issues we were having.
            if (x > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width && y > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height)
            {
                CursorImage.UpdateLocation(AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
                //Core.Screen.Screen.TheScreen.SaveFrame((uint)x, (uint)y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.SaveFrameFromBuffer((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.DrawFrameBlack(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, y);
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
            }
            else if (y > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height)
            {
                CursorImage.UpdateLocation(x, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
                //Core.Screen.Screen.TheScreen.SaveFrame((uint)x, (uint)y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.SaveFrameFromBuffer((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.DrawFrameBlack(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, y);
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Height - CursorImage.height);
            }
            else if (x > AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width)
            {
                CursorImage.UpdateLocation(AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, y);
                //Core.Screen.Screen.TheScreen.SaveFrame((uint)x, (uint)y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.SaveFrameFromBuffer((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.DrawFrameBlack(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, y);
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, AviumX.AviumCore.Screen.CreateScreen.TheScreen.Width - CursorImage.width, y);
            }
            else
            {
                CursorImage.UpdateLocation(x, y);
                //Core.Screen.Screen.TheScreen.SaveFrame((uint)x, (uint)y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.SaveFrameFromBuffer((uint)CursorImage.x, (uint)CursorImage.y, CursorImage.BackBuffer);
                //Core.Screen.Screen.TheScreen.DrawFrameBlack(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, y);
                AviumX.AviumCore.Screen.CreateScreen.TheScreen.DrawFrameBlackAndWhiteInBuffer(CursorImage.CursorImage, CursorImage.width, CursorImage.height, x, y);
            }
            //Core.Threading.Sleep.DoSleep(500);
        }


        /*
         * This is not needed, as the mouse will undraw it's self.
         */
        public void UndrawCursor()
        {

        }
        public static Boolean CheckMouseLeftClick(int x, int y, int width, int height)
        {
            if (Mouse.Getx() >= x && Mouse.Getx() <= x + width && Mouse.Gety() >= y && Mouse.Gety() <= y + height && CurrentState == MouseState.LeftClick)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean CheckMouseRightClick(int x, int y, int width, int height)
        {
            if (Mouse.Getx() >= x && Mouse.Getx() <= x + width && Mouse.Gety() >= y && Mouse.Gety() <= y + height && CurrentState == MouseState.RightClick)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean CheckMouseMiddleClick(int x, int y, int width, int height)
        {
            if (Mouse.Getx() >= x && Mouse.Getx() <= x + width && Mouse.Gety() >= y && Mouse.Gety() <= y + height && CurrentState == MouseState.MiddleClick)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
